package com.ccsoft.zknote.controller;

import cn.hutool.core.lang.Assert;
import cn.hutool.core.lang.Validator;
import cn.hutool.crypto.SecureUtil;
import com.ccsoft.zknote.enums.EventCode;
import com.ccsoft.zknote.exception.ServiceException;
import com.ccsoft.zknote.pojo.User;
import com.ccsoft.zknote.pojo.system.JsonResult;
import com.ccsoft.zknote.service.IUserService;
import com.ccsoft.zknote.util.SystemUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.sql.SQLException;
import java.util.Map;

/**
 * @author chanchaw
 * @create 2023-07-09 20:19
 */
@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private IUserService userService;
    @Autowired
    private SystemUtils systemUtils;
    @PostMapping("/login/email/password")
    public JsonResult login(@RequestBody Map<String,String> params) {
        String email = params.get("email");
        String password = params.get("password");
        if(Validator.isEmpty(email)) return JsonResult.error("邮箱不可为空！");
        if(Validator.isEmpty(password)) return JsonResult.error("密码不可为空！");

        password = SecureUtil.md5(password);
        Map<String, Object> loginRetMap = null;
        try {
            loginRetMap = userService.login(email, password);
        } catch (ServiceException e) {
            return JsonResult.error("使用邮箱密码注册账号失败！");
        }

        return JsonResult.ok(loginRetMap,"登录成功");
    }

    @PostMapping("/register")
    public JsonResult register(@RequestBody Map<String,String> params){
        String email = params.get("email");
        String vc = params.get("vc");
        String vcTokenKey = params.get("vcTokenKey");

        SystemUtils.notEmpty(email,"邮箱不可为空！");
        SystemUtils.notEmpty(vc,"验证码不可为空！");
        SystemUtils.notEmpty(vcTokenKey,"token不可为空！");

        return JsonResult.ok(userService.register(email, vc, vcTokenKey));
    }

    @PostMapping("/logout")
    public JsonResult logout(@RequestBody Map<String,String> params){
        String userToken = params.get("userToken");
        userService.logout(userToken);
        return JsonResult.ok(null,"用户注销成功");
    }

    @GetMapping("/test01")
    public JsonResult test01() throws SQLException, ClassNotFoundException {
        return JsonResult.ok(systemUtils.test01());
    }

    @GetMapping("/getDbInfoList")
    public JsonResult getDbInfoList() throws SQLException, ClassNotFoundException {
        return JsonResult.ok(systemUtils.getDbInfoList());
    }

    @GetMapping("/get8Email/{email}")
    public JsonResult getDbInfoList(@PathVariable String email) throws SQLException, ClassNotFoundException {
        return JsonResult.ok(userService.get8Email(email));
    }

    @GetMapping("/list")
    public JsonResult getList() throws SQLException, ClassNotFoundException {
        return JsonResult.ok(userService.getList());
    }

    @PostMapping("/insert")
    public JsonResult insert(@RequestBody User record) throws SQLException, ClassNotFoundException {
        return JsonResult.ok(userService.insert(record));
    }

    @PostMapping("/save")
    public JsonResult save(@RequestBody User record) throws SQLException, ClassNotFoundException {
        return JsonResult.ok(userService.save(record));
    }
}
